var util = require('../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
    hasData: false,
    vehicle_data: {}
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onLoad: function () {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--车主业务端" });
    wx.showToast({
      title: "数据加载中，请稍候...",
      icon: 'loading',
      duration: 3000
    });
    this.loadData();
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  loadData: function () {
    var that = this;
    wx.request({
      url: app.server+'/wechat/wxapplet/getMyVehicleData',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: {
        openid: app.globalData.openid
      },
      success: function (res) {
        wx.hideToast();
        var vehicle_data = res.data.data, _vehicle_data = [];
        if (res.data.success && vehicle_data.length > 0) {
          that.setData({
            hasData: true
          }); console.log(vehicle_data)
          for (var i = 0; i < vehicle_data.length; i++) {
            _vehicle_data.push({
              ID: vehicle_data[i].ID,
              plate: vehicle_data[i].plate,
              driver_name: vehicle_data[i].driver_name||"无",
              driver_phone: vehicle_data[i].driver_phone,
              latitude_current: vehicle_data[i].latitude_current,
              longitude_current: vehicle_data[i].longitude_current
            });
          };
          that.setData({
            vehicle_data: _vehicle_data
          });

        } else {
          that.setData({
            hasData: false
          })
        };
      },
      fail: function (error) {
        wx.hideToast();
        console.log(error);
        that.setData({
          hasData: false
        })
      }
    })
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    console.log(e.detail.value)
    this.setData({
      inputVal: e.detail.value
    });
  },
  calltodriver: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.target.id
    })
  },
  driverLocation: function (e) {
    var location = String(e.target.id).split("_");
    wx.openLocation({
      latitude: location[0],
      longitude: location[1],
      scale: 28
    })
  },
  gotoService: function () {
    wx.redirectTo({
      url: '/pages/vehicle/service'
    })
  },
  gotoIncome: function () {
    wx.redirectTo({
      url: '/pages/vehicle/income'
    })
  }
})
